class Enemy extends Ship
{
  void draw(Camera c)
  {
    if( ((position.x + rad) > c.position.x - (width/2)) && ((position.x - rad) < c.position.x + (width/2)) && ((position.y + rad) > c.position.y - (height/2)) && ((position.y - rad)< c.position.y + (height/2)))
    {
      fill(100,100,100);
      stroke(100,100,100);
      pushMatrix();
      translate(position.x - width / 2 - c.position.x + width/2, position.y - height / 2 - c.position.y + height/2);
      float drawX = (cos(direction + HALF_PI)*32) - (cos(direction)*sqrt(1024));
      float drawY = (sin(direction + HALF_PI)*32) - (sin(direction)*sqrt(1024));
      shape(shipShape, (width/2)  + drawX ,(height/2) + drawY ,64,64);
      popMatrix();
    }


  }
    

  
  void update()
  {
    checkHealth();
    if(alive)
    {
      artificiallyIntelligentize();
      updatePosition();
      if(firing)
        queueFire();
      applyPlanetGravity();
      applyPlayerGravity(player);
      checkCollisions();
    }
  }
  
      void fireBullet()
    {
      if(energy >= 10)
      {
      PVector newV = new PVector(-cos(direction),-sin(direction),0);
      newV.normalize();
      newV.mult(10);
      addGameObject(new Bullet((int)(position.x - cos(direction)*2*rad), (int)(position.y - sin(direction)*2*rad), 25, newV));
      particleCount++;
      //energy -= 10;
      }
    }
  
  void turnRight()
  {
    if(alive)
        direction += turnSpeed;
        rotateShapes(turnSpeed);
  }
  void turnLeft()
  {
    if(alive)
        direction -= turnSpeed;
        rotateShapes(-turnSpeed);
  }
  
  void rotateShapes(float rot)
  {
    shipShape.rotate(rot);
  }
  void artificiallyIntelligentize()
  {
    float angle = atan2(player.position.y-position.y, player.position.x-position.x);
    float distanceToPlayer = sqrt(sq(position.x - player.position.x) + sq(position.y - player.position.y));
    
    float newDirection = angle + PI;
    rotateShapes(newDirection - direction);
    direction = newDirection;
    
    if(distanceToPlayer > 300)
    {
    accelerate();
        firing = false;
    }
    if(distanceToPlayer < 150)
    {
    decelerate();
    firing = false;
    }
    else
    {
      firing = true;
    }
  }
  
  Enemy(int xin, int yin, int massin)
  {
    super(xin, yin, massin);
    topSpeed = 6;
    shipShape = loadShape("gfx/enemyShip.svg");
    rotateShapes(direction - HALF_PI);
  }
}
